# -*- coding: utf-8 -*-
# @Time: 2021/8/23 9:00
# @File: arg-1958.py
# @DESC:


# 寻找数组差值最大的区间--两次循环，超出时间限制
def maxProfit(prices):
    # 低进，高出
    lent = len(prices)
    max_profit = 0
    for i in range(lent):
        for j in range(i + 1, lent):
            max_profit = max(max_profit, prices[j] - prices[i])
    return max_profit


# 历史最低点买入
def maxProfit2(prices):
    max_price = min_price = prices[0]
    min_price_index = 0
    for index, i in enumerate(prices):
        if i < min_price and index < len(prices) - 1:
            min_price = i
            max_price = i
            min_price_index = index
        if index > min_price_index and i > max_price:
            max_price = i
        print(min_price, max_price)
    return max(0, max_price - min_price)


# 遍历一次，记录最大值和最小值
def maxProfit3(prices):
    max_profit = 0
    min_price = prices[0]
    for i in prices:
        if i < min_price:
            min_price = i
        if i - min_price > max_profit:
            max_profit = i - min_price
    return max_profit


prices = [7,1,5,3,6,4]
r = maxProfit3(prices)
print(r)
